package com.pshare.artemis.common.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.InitiateMultipartUploadRequest;
import com.aliyun.oss.model.InitiateMultipartUploadResult;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PartETag;
import com.aliyun.oss.model.UploadPartRequest;
import com.aliyun.oss.model.UploadPartResult;

@Service
public class AliYunUpService {
	//限制上传大小
	public static  final Long MAXSIZE = 10*1024*1024L;
	
	/**
	 * 向阿里云上传文件(事先知道文件大小)
	 * @param clien 阿里云连接对象
	 * @param user 当前操作用户
	 * @param bucketName 根目录
	 * @param key 文件后缀名
	 * @param file 文件流
	 * @param fileType 文件的ContentType类型
	 * @return
	 * @throws IOException 
	 */
	public void upImage(OSSClient clien,String bucketName, 
			String key, InputStream content,String fileType) throws IOException{
	    // 创建上传Object的Metadata
	    ObjectMetadata meta = new ObjectMetadata();
	    int t=content.available();
	    // 必须设置ContentLength
	    meta.setContentLength((long)t);
	    meta.setContentType(fileType);
	    // 上传Object.
	    clien.putObject(bucketName, key, content, meta);
	}
	/**
	 * 阿里云文件上传（事先不知道文件大小）
	 * @param client
	 * @param bucketName
	 * @param key
	 * @param partFile
	 * @return
	 * @throws IOException
	 */
	public List<PartETag> putMultipart(OSSClient client,String bucketName,String key,File partFile) throws IOException{
		// 开始Multipart Upload
		InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
		InitiateMultipartUploadResult initiateMultipartUploadResult = client.initiateMultipartUpload(initiateMultipartUploadRequest);
		// 设置每块为 5M
		final int partSize = 1024 * 1024 * 5;
		// 计算分块数目
		int partCount = (int) (partFile.length() / partSize);
		if (partFile.length() % partSize != 0){
		    partCount++;
		}
		// 新建一个List保存每个分块上传后的ETag和PartNumber
		List<PartETag> partETags = new ArrayList<PartETag>();
		for(int i = 0; i < partCount; i++){
		    // 获取文件流
		    FileInputStream fis = new FileInputStream(partFile);
		    // 跳到每个分块的开头
		    long skipBytes = partSize * i;
		    fis.skip(skipBytes);
		    // 计算每个分块的大小
		    long size = partSize < partFile.length() - skipBytes ?
		            partSize : partFile.length() - skipBytes;
		    // 创建UploadPartRequest，上传分块
		    UploadPartRequest uploadPartRequest = new UploadPartRequest();
		    uploadPartRequest.setBucketName(bucketName);
		    uploadPartRequest.setKey(key);
		    uploadPartRequest.setUploadId(initiateMultipartUploadResult.getUploadId());
		    uploadPartRequest.setInputStream(fis);
		    uploadPartRequest.setPartSize(size);
		    uploadPartRequest.setPartNumber(i + 1);
		    UploadPartResult uploadPartResult = client.uploadPart(uploadPartRequest);
		    // 将返回的PartETag保存到List中。
		    partETags.add(uploadPartResult.getPartETag());
		    // 关闭文件
		    fis.close();
		}
		return partETags;
	}
}
